﻿#region Header

/*
    This file is part of NDoctor.

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.AspTest.Domain.Components
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Probel.AspTest.Domain.Entities;

    internal class ChartComponent : BaseComponent, IChartComponent
    {
        #region Methods

        /// <summary>
        /// Get a graph of the average execution time of the methods by day
        /// </summary>
        /// <returns>A chart to be dicplayed</returns>
        public ChartData<DateTime, double> ExecutionTimeGraph()
        {
            var points = (from a in this.QueryStatistics
                          group a by a.TimeStamp.Date into g
                          select new ChartPoint<DateTime, double>(g.Key, g.Average(e => e.ExecutionTime)))
                            .ToList();
            return new ChartData<DateTime, double>(points.OrderBy(e => e.X));
        }

        /// <summary>
        /// Gets the average execution time of a method of a component.
        /// </summary>
        /// <returns>The average execution time of the methods</returns>
        public double GetAvgExecutionTime()
        {
            var result = (from a in this.QueryStatistics
                          select a.ExecutionTime).ToList();

            return (result.Count != 0) ? result.Average() : 0;
        }

        /// <summary>
        /// Gets the average execution time by methods.
        /// </summary>
        /// <returns>A chart to be dicplayed</returns>
        public ChartData<string, double> GetAvgExecutionTimeGraph()
        {
            var points = (from a in this.QueryStatistics
                          group a by a.MethodName into g
                          select new ChartPoint<string, double>(g.Key, g.Average(e => e.ExecutionTime)))
                                .ToList();

            return new ChartData<string, double>(points.OrderBy(e => e.Y));
        }

        /// <summary>
        /// Gets the bottlenecks by methods.
        /// </summary>
        /// <returns>A chart to be dicplayed</returns>
        public ChartData<string, double> GetBottlenecks()
        {
            var points = (from a in this.QueryStatistics
                          where a.IsPossibleBottleneck == true
                          group a by a.MethodName into g
                          select new ChartPoint<string, double>(g.Key, g.Count()))
                            .ToList();
            return new ChartData<string, double>(points.OrderByDescending(e => e.Y));
        }

        /// Gets the average execution time by method. Provides more information than the <see cref="GetAvgExecutionTimeGraph"/>
        /// </summary>
        /// <returns>A list with all the information about execution time by method</returns>
        public IEnumerable<Bottleneck> GetBottlenecksArray()
        {
            var list = (from a in this.QueryStatistics
                        where a.IsPossibleBottleneck == true
                        group a by new { a.MethodName, a.TargetTypeName } into g
                        select new Bottleneck()
                        {
                            MethodName = g.Key.MethodName,
                            Count = g.Count(),
                            AvgExecutionTime = g.Average(e => e.ExecutionTime) / 1000,
                            AvgThreshold = g.Average(e => e.Threshold) / 1000,
                            TargetTypeName = g.Key.TargetTypeName,
                        }).ToList();

            return list.OrderByDescending(e => e.Count);
        }

        /// <summary>
        /// Gets the date of the usages the by component.
        /// </summary>
        /// <returns>
        /// A chart containing the data to be displayed in a chart
        /// </returns>
        public ChartData<string, double> GetUsageByComponent()
        {
            var points = (from a in this.QueryStatistics
                          group a by a.TargetTypeName into g
                          select new ChartPoint<string, double>(g.Key, g.Count()))
                                .ToList();
            return new ChartData<string, double>(points.OrderBy(e => e.Y));
        }

        /// <summary>
        /// Gets the number of times the methods have been called.
        /// </summary>
        /// <returns>A chart to be dicplayed</returns>
        public ChartData<string, double> GetUsageByMethods()
        {
            var points = (from a in this.QueryStatistics
                          group a by a.MethodName into g
                          select new ChartPoint<string, double>(g.Key, g.Count()))
                                .ToList();
            return new ChartData<string, double>(points.OrderBy(e => e.Y));
        }

        #endregion Methods
    }
}